/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QVBoxLayout>
#include <QListView>

#include "enterchannelnumber.h"
#include "channellistmodel.h"


EnterChannelNumber::EnterChannelNumber( const QString &key, MainWindow *mainWindow, QWidget *parent ) : QDialog( parent ),
    m_mainWindow( mainWindow )
{
    int max = mainWindow->channelList()->channelFromRow( mainWindow->channelList()->size() - 1 );
    setWindowTitle( tr( "Channel" ) );

    QValidator *validator = new QIntValidator( 1, max, this );
    m_lineEdit = new QLineEdit( key, this );
    m_lineEdit->setValidator( validator );

    QVBoxLayout *vBox = new QVBoxLayout;
    vBox->addWidget( m_lineEdit );
    setLayout( vBox );

    m_timer = new QTimer( this );
    connect( m_timer, SIGNAL( timeout() ), this, SLOT( closeDialog() ) );
    m_timer->start( 1500 );

    connect( m_lineEdit, SIGNAL( returnPressed() ), this, SLOT( closeDialog() ) );
    //Restart close timer when something was entered
    connect( m_lineEdit, SIGNAL( textChanged( const QString& ) ), m_timer, SLOT( start() ) );
}


void EnterChannelNumber::closeDialog()
{
    qint32 row = m_mainWindow->channelList()->rowFromChannel( m_lineEdit->text().toInt() );
    if ( row >= 0 )
    {
        m_mainWindow->channelListSelectionModel()->setCurrentIndex( m_mainWindow->channelListModel()->index( row, 0 ),
                                                                    QItemSelectionModel::SelectCurrent |
                                                                    QItemSelectionModel::Rows );

        accept();
    }
    else
    {
        reject();
    }
}
